#![allow(dead_code)]

struct MutLrn {}

impl MutLrn {
    pub fn hello() {
        println!("Mut learning!");
    }
}

#[cfg(test)]
mod tests {
    #[test]
    #[allow(unused_assignments)]
    fn mut_test() {
        // i32和&i32是两种类型
        let mut a = 1;
        a = 2;
        let b = &mut a;
        *b = 3;
        assert_eq!(a, 3);
    }

    #[test]
    /// NOTE: mut 匹配模式用mut 借用
    /// 参考ref在匹配模式中的用法
    fn match_mut_test() {
        let mut maybe_name = Some(String::from("Alice"));
        // 匹配模式将ref换成&, 会报错
        if let Some(ref mut name) = maybe_name {
            *name = String::from("Bob")
        }
        assert_eq!(maybe_name, Some(String::from("Bob")));
    }

    #[test]
    #[allow(unused_variables)]
    #[allow(unused_assignments)]
    /// NOTE: mut a:&T 与 a:&mut T 的区别
    /// mut a:&T 是a为可变
    /// a:&mut T 是T为可变借用. &mut T 一般都是一个复杂可变数据结构
    fn diff_mut_test() {
        struct Foo(usize);

        let mut a: &Foo = &Foo(0);
        a = &Foo(1);
        // a.0 = 2;

        let b: &mut Foo = &mut Foo(0);
        // b = &mut Foo(1);
        b.0 = 2;

        let mut arr = [1, 2, 3];
        let c = &mut arr;
        arr[0] = 10;
        assert_eq!(arr, [10, 2, 3]);
        // println!("{:?}", arr);
    }
}
